package com.ymatou.iapi.refund.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.refund.parameter.AddReturnExpressCodeBean;
import com.ymatou.iapi.refund.parameter.CreateReturnBillBean;
import com.ymatou.iapi.refund.service.AddReturnExpressCodeCall;
import com.ymatou.iapi.refund.service.CreateReturnBillCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.RefundCallService;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.database.model.CS_ReturnApplyBill;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sqlwapper.CS_ReturnApplyBillWapper;
/**
*撤销申请
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_AddReturnExpressCode{
	private static AddReturnExpressCodeBean bean;
	private static AddReturnExpressCodeCall call;
	private static CreateReturnBillBean createBean=new CreateReturnBillBean();
	private static CreateReturnBillCall createCall=new CreateReturnBillCall();
	private OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	private TradingServiceResp tradingServiceResp=new TradingServiceResp();
	private CS_ReturnApplyBillWapper applyWapper=new CS_ReturnApplyBillWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("撤销申请");
	}

	@Before
	public void caseUp() {
		bean=new AddReturnExpressCodeBean();
		call=new AddReturnExpressCodeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddReturnExpressCode_001() {
		Logger.start(true,"正例-添加单号");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService=new TradingPlaceOrderCallService();
			RefundCallService service=new RefundCallService();
			TradingServiceResp tradingServiceResp=tradingService.placeOrderWithTwoProcutInOneOrder(false, false);
			createBean=service.setCreateReturnBillBean_AcceptOrder(tradingServiceResp,tradingService,false);
			service.callCreateReturnBill(createBean,createCall);
			
			//根据userId 获取token
//			String token=userWapper.getAccessTokenByUid(createBean.getUserid());
//			bean.setAccessToken(token);
//			bean.setCallback("");
			
			bean.setUserId(createBean.getUserid());
			bean.setReturnId(createCall.getCallData().getReturnBillList().get(0).getReturnId());
			//bean.setReturnId("TH180627000006268");
			bean.setExpressBillNumber("111");
			bean.setExpressCoCode("222");
			bean.setExpressCoName("韵达快递");
			//action=11 审核通过    type=1 退货退款
			applyWapper.updateActionByReturnId(bean.getReturnId(),11);
			applyWapper.updateTypeByReturnId(bean.getReturnId(),1);
			call.setData(bean);
			call.callService();			
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddReturnExpressCode_002() {
		Logger.start(false,"反例-添加单号-审核未通过");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService=new TradingPlaceOrderCallService();
			RefundCallService service=new RefundCallService();
			TradingServiceResp tradingServiceResp=tradingService.placeOrderWithTwoProcutInOneOrder(false, false);
			createBean=service.setCreateReturnBillBean_AcceptOrder(tradingServiceResp,tradingService,false);
			service.callCreateReturnBill(createBean,createCall);
			
			bean.setUserId(createBean.getUserid());
			bean.setReturnId(createCall.getCallData().getReturnBillList().get(0).getReturnId());
			bean.setExpressBillNumber("111");
			bean.setExpressCoCode("222");
			bean.setExpressCoName("韵达快递");
			//action=11 审核通过    type=1 退货退款
			//applyWapper.updateActionByReturnId(bean.getReturnId(),11);
			applyWapper.updateTypeByReturnId(bean.getReturnId(),1);
			call.setData(bean);
			call.callService();			

			Logger.verifyEquals(true,call.getMsg1().contains("退单未审核"), "验证Msg1");
			Logger.verifyEquals("false",call.getRetData(), "验证RetData");
			Logger.verifyEquals("7",call.getbCode(), "验证bCode");
			Logger.verifyEquals("500",call.getStatus(), "验证Status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddReturnExpressCode_003() {
		Logger.start(false,"反例-添加单号-退单未选择退货");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService=new TradingPlaceOrderCallService();
			RefundCallService service=new RefundCallService();
			TradingServiceResp tradingServiceResp=tradingService.placeOrderWithTwoProcutInOneOrder(false, false);
			createBean=service.setCreateReturnBillBean_AcceptOrder(tradingServiceResp,tradingService,false);
			service.callCreateReturnBill(createBean,createCall);
			
			bean.setUserId(createBean.getUserid());
			bean.setReturnId(createCall.getCallData().getReturnBillList().get(0).getReturnId());
			bean.setExpressBillNumber("111");
			bean.setExpressCoCode("222");
			bean.setExpressCoName("韵达快递");
			//action=11 审核通过    type=1 退货退款
			applyWapper.updateActionByReturnId(bean.getReturnId(),11);
			//applyWapper.updateTypeByReturnId(bean.getReturnId(),1);
			call.setData(bean);
			call.callService();			

			Logger.verifyEquals(true,call.getMsg1().contains("退单未选择退货"), "验证Msg1");
			Logger.verifyEquals("false",call.getRetData(), "验证RetData");
			Logger.verifyEquals("7",call.getbCode(), "验证bCode");
			Logger.verifyEquals("500",call.getStatus(), "验证Status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddReturnExpressCode_004() {
		Logger.start(false,"反例-添加单号-用户不存在");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService=new TradingPlaceOrderCallService();
			RefundCallService service=new RefundCallService();
			TradingServiceResp tradingServiceResp=tradingService.placeOrderWithTwoProcutInOneOrder(false, false);
			createBean=service.setCreateReturnBillBean_AcceptOrder(tradingServiceResp,tradingService,false);
			service.callCreateReturnBill(createBean,createCall);
			
			bean.setUserId(999999);
			bean.setReturnId(createCall.getCallData().getReturnBillList().get(0).getReturnId());
			bean.setExpressBillNumber("111");
			bean.setExpressCoCode("222");
			bean.setExpressCoName("韵达快递");
			//action=11 审核通过    type=1 退货退款
			applyWapper.updateActionByReturnId(bean.getReturnId(),11);
			applyWapper.updateTypeByReturnId(bean.getReturnId(),1);
			call.setData(bean);
			call.callService();			

			Logger.verifyEquals(true,call.getMsg1().contains("无效的用户"), "验证Msg1");
			Logger.verifyEquals("false",call.getRetData(), "验证RetData");
			Logger.verifyEquals("4",call.getbCode(), "验证bCode");
			Logger.verifyEquals("500",call.getStatus(), "验证Status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddReturnExpressCode_005() {
		Logger.start(false,"反例-添加单号-退单不存在");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService=new TradingPlaceOrderCallService();
			RefundCallService service=new RefundCallService();
			TradingServiceResp tradingServiceResp=tradingService.placeOrderWithTwoProcutInOneOrder(false, false);
			createBean=service.setCreateReturnBillBean_AcceptOrder(tradingServiceResp,tradingService,false);
			service.callCreateReturnBill(createBean,createCall);
			
			bean.setUserId(createBean.getUserid());
			bean.setReturnId("999999");
			bean.setExpressBillNumber("111");
			bean.setExpressCoCode("222");
			bean.setExpressCoName("韵达快递");
			//action=11 审核通过    type=1 退货退款
			applyWapper.updateActionByReturnId(bean.getReturnId(),11);
			applyWapper.updateTypeByReturnId(bean.getReturnId(),1);
			call.setData(bean);
			call.callService();			

			Logger.verifyEquals(true,call.getMsg1().contains("退单不存在"), "验证Msg1");
			Logger.verifyEquals("false",call.getRetData(), "验证RetData");
			Logger.verifyEquals("1",call.getbCode(), "验证bCode");
			Logger.verifyEquals("201",call.getStatus(), "验证Status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void verifyReturn()
	{
		Logger.verifyEquals("200",call.getStatus(), "验证Status");
		Logger.verifyEquals("",call.getMsg1(), "验证Msg1");
		Logger.verifyEquals("true",call.getRetData(), "验证RetData");
		Logger.verifyEquals("0",call.getbCode(), "验证bCode");
		CS_ReturnApplyBill applyModel=applyWapper.selectByApplyBillId(bean.getReturnId()).get(0);
		Logger.verifyEquals(bean.getExpressBillNumber(),applyModel.getExpressBillNumber(), "验证数据库CS_ReturnApplyBill 字段ExpressBillNumber是否正确");
		Logger.verifyEquals(bean.getExpressCoCode(),applyModel.getExpressCoCode(), "验证数据库CS_ReturnApplyBill 字段ExpressCoCode是否正确");
		Logger.verifyEquals(bean.getExpressCoName(),applyModel.getExpressCoName(), "验证数据库CS_ReturnApplyBill 字段ExpressCoName是否正确");
	}
}